-
Notifications
You must be signed in to change notification settings - Fork 265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test(log): new unit tests #7988
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too many of these tests are not related to captureLogs()
, if you want to keep them, move them to another commit & file.
@xen-orchestra/log/capture.test.js
Outdated
assert.equal(logsTransportDefault[0]?.message, expected1.message) | ||
assert.equal(typeof logsTransportDefault[0]?.level, 'number', `level attribute should exist`) | ||
assert.ok(typeof logsTransportDefault[0]?.namespace === 'string', `namespace attribute should exist`) | ||
assert.ok(logsTransportDefault[0]?.time instanceof Date, `time attribute should exist`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not use optional chaining when you know the entry exist.
@xen-orchestra/log/capture.test.js
Outdated
} | ||
logger.debug(expected1.message) | ||
assert.equal(logsTransportDefault[0]?.message, expected1.message) | ||
assert.equal(typeof logsTransportDefault[0]?.level, 'number', `level attribute should exist`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not bother adding an assert message unless it brings a new info.
In this case, I don't see how it would really help, if the test fails, we know which assert did and we fix it.
To be clear, it's absolutely not a problem that you provided a message, but you don't have to 🙂
@xen-orchestra/log/capture.test.js
Outdated
logger.debug(expected1.message) | ||
assert.equal(logsTransportDefault[0]?.message, expected1.message) | ||
assert.equal(typeof logsTransportDefault[0]?.level, 'number', `level attribute should exist`) | ||
assert.ok(typeof logsTransportDefault[0]?.namespace === 'string', `namespace attribute should exist`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use assert.equal
instead because it will show the actual value which is important to understand the issue when the assert fails.
@xen-orchestra/log/capture.test.js
Outdated
logsTransportFatal.length = 0 | ||
}) | ||
|
||
it('should log test with all its attributes', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's not what you are testing here: you are testing that logs not in captureLogs()
are handled by the fallback transport.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested what appeared in the Usage.md, here it was the "producer" part presenting the logger.
@xen-orchestra/log/capture.test.js
Outdated
assert.ok(typeof logsTransportDefault[0]?.namespace === 'string', `namespace attribute should exist`) | ||
assert.ok(logsTransportDefault[0]?.time instanceof Date, `time attribute should exist`) | ||
}) | ||
it('should log test 1 and 2', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what you are testing here and how it is related to captureLogs
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested in order what was in the Usage.md, there it was the "producer" part presenting the logger.
@xen-orchestra/log/capture.test.js
Outdated
it('should not dedup logs', () => { | ||
configure( | ||
createCaptureTransport([ | ||
{ | ||
transport: transportTest, | ||
}, | ||
]) | ||
) | ||
|
||
for (let i = 0; i < 3; i++) { | ||
logger.debug('this line should be logged 3 times') | ||
} | ||
assert.equal(logsTransportDefault.length, 3) | ||
}) | ||
it('should dedup logs', () => { | ||
configure( | ||
createCaptureTransport([ | ||
dedupe({ | ||
timeout: 50, // without a defined timeout, the test will wait for 10 minutes | ||
transport: transportTest, | ||
}), | ||
]) | ||
) | ||
|
||
for (let i = 0; i < 3; i++) { | ||
logger.debug('this line should be logged only once') | ||
} | ||
assert.equal(logsTransportDefault.length, 1) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Out of captureLogs()
scope, you can move into its own file.
@xen-orchestra/log/logger.test.js
Outdated
const expected1 = { | ||
level: 20, | ||
namespace: 'test-logger', | ||
message: 'test 1 with all its attributes', | ||
time: new Date(), | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you creating this object? you are not using it? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The message is tested line 49 :
assert.equal(logsTransportDefault[0].message, expected1.message)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but you don't need this object, you can simply create a message
variable if that's all you need.
assert.equal(typeof logsTransportDefault[0].namespace, 'string', `namespace attribute should exist`) | ||
assert.equal(logsTransportDefault[0].time instanceof Date, true, 'time attribute should exist') | ||
}) | ||
it('should log test 1 and 2', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What feature does this case test that the the previous one did not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It test the "test transport" can contain many logs, and not only one constantly overwriten. Needed by the captureLog test after when we test multiple logs.
d2497fa
to
e0400e7
Compare
6131041
to
1e051bd
Compare
e0400e7
to
d13fc22
Compare
b9ef692
to
defa4ae
Compare
2bc6ddf
to
f41bcbb
Compare
f41bcbb
to
4197370
Compare
162703f
to
79d0e56
Compare
Co-authored-by: Julien Fontanet <[email protected]>
Co-authored-by: Julien Fontanet <[email protected]>
Co-authored-by: Julien Fontanet <[email protected]>
79d0e56
to
8b41248
Compare
Description
write unit tests for logCapture
XO-184
Checklist
Fixes #007
,See xoa-support#42
,See https://...
)Introduced by
CHANGELOG.unreleased.md